package com.android.aishangauction.fragment.member;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.aishangauction.R;
import com.android.aishangauction.activity.AuctioningPayOrderActivity;
import com.android.aishangauction.activity.GoodsDetailActivity;
import com.android.aishangauction.activity.mine.LoginActivity;
import com.android.aishangauction.activity.mine.OrderToShare;
import com.android.aishangauction.adapt.listMineOrderAdapt;
import com.android.aishangauction.model.auction.AuctionActivityData;
import com.android.aishangauction.model.auction.AuctionActivitySendData;
import com.android.aishangauction.model.auction.AuctionOrderListData;
import com.android.aishangauction.model.auction.AuctionOrderListDataCallback;
import com.android.aishangauction.model.auction.Auction_JoinBackData;
import com.android.aishangauction.model.auction.Auction_detail_rule;
import com.android.aishangauction.model.auction.Auction_detail_ruleCallback;
import com.android.aishangauction.model.auction.UpdateAuctionBean;
import com.android.aishangauction.model.data;
import com.android.aishangauction.model.dataCallback;
import com.android.aishangauction.service.WebSocketService2;
import com.android.aishangauction.utils.ConstantsUrl;
import com.android.aishangauction.utils.HttpUtils;
import com.android.aishangauction.utils.LoginState;
import com.android.aishangauction.utils.MyLog;
import com.android.aishangauction.utils.ObjectUtils;
import com.android.aishangauction.view.dialog.MainMsg;
import com.android.aishangauction.view.recycleview.CustomLoadMoreView;
import com.android.aishangauction.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.android.aishangauction.R.id.no_data;


/**
 * 订单列表
 * ---------------------------------------
 */
public class Fragment_Mine_Order extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.listView)
    CustomRecycleView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.gotowander_fm)
    FrameLayout gotowander_fm;
    @BindView(no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress_loading)
    FrameLayout progressLoading;
    private View rootView;

    private static final String ARG_PARAM1 = "param1";

    private String mParam1;


    //控件是否已经初始化
    private boolean isPrepared;
    private listMineOrderAdapt adapter;
    private ArrayList<AuctionOrderListData.DataBean.ListBean> listdata = new ArrayList<>();
    //“全部”数据
    private ArrayList<AuctionOrderListData.DataBean.ListBean> merge_listdata = new ArrayList<>();
    //正在趣买这块用来保存更新的数据的id作为key
    private ArrayList<AuctionActivityData.ListBean> temporary_listdata = new ArrayList<>();
    private HashMap<String,UpdateAuctionBean> updateBean = new HashMap<>();
    private AuctionActivitySendData auctionActivitySendData = new AuctionActivitySendData();
    private AuctionActivityData auctionactivitydata;
    private Auction_JoinBackData auction_joinBackData = new Auction_JoinBackData();
    private WebSocketService2 service;
    private BroadcastReceiver mBroadcastReceiver;
    private Gson gson = new Gson();
    private String gsonstring="";
    private Message message3;
    private boolean isAucDataEnd = false;
    private boolean isNeedMerge = false;
    private int remainsize;
    private int page = 1;
    private boolean networkConnected;
    private FragmentActivity mcontext;
    private int mid;
    private MainMsg mainMsg;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);

        }
    }

    public static Fragment_Mine_Order newInstance(String param1) {
        Fragment_Mine_Order fragment = new Fragment_Mine_Order();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    private void initUI() {
        adapter = new listMineOrderAdapt(R.layout.item_list_mine_order, null);
        adapter.setMid(mid+"");
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.setOnLoadMoreListener(this, mRecyclerView);
        final LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                if((mid+"").equals(adapter.getItem(position).getMid())) {
                    Intent intent = new Intent(mcontext, AuctioningPayOrderActivity.class);
                    intent.putExtra("order_id", adapter.getItem(position).getId());
                    intent.putExtra("address_id", adapter.getItem(position).getAddress_id());
                    mcontext.startActivity(intent);
                }
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public boolean onItemChildClick(BaseQuickAdapter adapter1, View view, final int position) {

                switch (view.getId()) {
                    case R.id.waiting_topay_tv:
                        if((mid+"").equals(adapter.getItem(position).getMid())) {
                            switch (adapter.getItem(position).getOrder_code()) {
                                case 1:
                                    Intent intent = new Intent(mcontext, AuctioningPayOrderActivity.class);
                                    intent.putExtra("order_id", adapter.getItem(position).getId());
                                    intent.putExtra("address_id", adapter.getItem(position).getAddress_id());
                                    mcontext.startActivity(intent);
                                    break;
                                case 2:
                                    Intent intent1 = new Intent(mcontext, AuctioningPayOrderActivity.class);
                                    intent1.putExtra("order_id", adapter.getItem(position).getId());
                                    intent1.putExtra("address_id", adapter.getItem(position).getAddress_id());
                                    mcontext.startActivity(intent1);
                                    break;
                                case 4:
                                    mainMsg = new MainMsg(mcontext);
                                    mainMsg.show(mcontext.getWindow().getDecorView(), 0, "是否确认收到货物？");
                                    mainMsg.setOnSetSureClickListener(new MainMsg.OnSetSureClickListener() {
                                        @Override
                                        public void onSure() {
                                            MakeSureOrder(position, adapter.getItem(position).getId());
                                            mainMsg.close();
                                        }
                                    });
                                    break;
                                case 5:
                                    Intent intent_address1 = new Intent(mcontext, OrderToShare.class);
                                    intent_address1.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID, adapter.getItem(position).getId());
                                    intent_address1.putExtra("order_thumbs", adapter.getItem(position).getThumb());
                                    intent_address1.putExtra("order_title", adapter.getItem(position).getTitle());
                                    intent_address1.putExtra("order_price", adapter.getItem(position).getPrice());
                                    startActivity(intent_address1);
                                    break;
                            }
                        }
                        break;
                    case R.id.go_on_auctioning_ll:
                        if (LoginState.getInstance().isLogin(mcontext)) {
                            Intent intent = new Intent(mcontext, GoodsDetailActivity.class);
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_SHOW, 1);
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_ID,temporary_listdata.get(position).getId());
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_COMMON_ID,"");
                            startActivityForResult(intent,1);
                        }
                        else
                        {
                            startActivityForResult(new Intent(mcontext, LoginActivity.class),2);
                        }
                        break;

                }
                return false;
            }
        });


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        networkConnected = ObjectUtils.isNetworkConnected(getActivity());
        rootView = inflater.inflate(R.layout.layout_recycle_refresh_mineauction, container, false);
        ButterKnife.bind(this, rootView);
        mcontext = getActivity();
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if(!LoginState.getInstance().getid(mcontext).equals("")&&LoginState.getInstance().getid(mcontext)!=null) {
            mid = Integer.parseInt(LoginState.getInstance().getid(mcontext));
        }
        initUI();
        isPrepared = true;
        if(mParam1.equals("100")||mParam1.equals("0")) {
            broadcastReceive();
        }
        setlazyLoad();
        return rootView;
    }
    /**
     * 确认订单
     */
    private void MakeSureOrder(final int position, String order_id) {
        networkConnected = ObjectUtils.isNetworkConnected(getActivity());
        String address = ConstantsUrl.domain + ConstantsUrl.AUCTIONMAKESURE;
        HashMap<String, String> map = new HashMap<>();
        map.put("order_id",order_id);
        if (networkConnected) {
            HttpUtils.post(address, map, new Auction_detail_ruleCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(Auction_detail_rule response, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        ObjectUtils.toast(getActivity(), "确认成功");
                        adapter.getItem(position).setOrder_code(5);
                        adapter.notifyDataSetChanged();
                    } else {
                        ObjectUtils.toast(getActivity(), "确认收货失败");
                    }
                }
            });
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(getActivity(), "网络连接出现异常");
        }
    }

    //设置广播监听
    private void broadcastReceive() {
        service = WebSocketService2.getInstance();

        mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                //更新商品列表
                if(action.equals(WebSocketService2.ACTION_AUCTION))
                {
                    auctionactivitydata = (AuctionActivityData) intent.getBundleExtra("bundle2").getSerializable("AuctionGetAuctions");
                    if(auctionactivitydata.getList()!=null) {
                        if(page==1)
                        {
                            temporary_listdata.clear();
                        }
                        listdata= new ArrayList<>();
                        for (int i = 0; i < auctionactivitydata.getList().size(); i++) {
                            listdata.add(new AuctionOrderListData.DataBean.ListBean(AuctionOrderListData.DataBean.ListBean.THREE, auctionactivitydata.getList().get(i).getId()
                                    , auctionactivitydata.getList().get(i).getPeriod(), auctionactivitydata.getList().get(i).getTitle()
                                    , auctionactivitydata.getList().get(i).getThumb(), auctionactivitydata.getList().get(i).getPrice()
                                    , auctionactivitydata.getList().get(i).getTime() * 1000, auctionactivitydata.getList().get(i).getStart() * 1000
                                    , auctionactivitydata.getList().get(i).getMid(), auctionactivitydata.getList().get(i).getUsername()));
                            updateBean.put(auctionactivitydata.getList().get(i).getId(),new UpdateAuctionBean());
                            temporary_listdata.add(new AuctionActivityData.ListBean(AuctionOrderListData.DataBean.ListBean.THREE, auctionactivitydata.getList().get(i).getId()
                                    , auctionactivitydata.getList().get(i).getPeriod(), auctionactivitydata.getList().get(i).getTitle()
                                    , auctionactivitydata.getList().get(i).getThumb(), auctionactivitydata.getList().get(i).getPrice()
                                    , auctionactivitydata.getList().get(i).getTime() * 1000, auctionactivitydata.getList().get(i).getStart() * 1000
                                    , auctionactivitydata.getList().get(i).getMid(), auctionactivitydata.getList().get(i).getUsername()));
                        }
                        adapter.setUpdateBean(updateBean);
                        AdapterListData();
                    }
                    else
                    {
                        noData.setVisibility(View.VISIBLE);
                        mRecyclerView.setVisibility(View.INVISIBLE);
                    }
                }
                //更新商品价格，出价人
                if(action.equals(WebSocketService2.ACTION_JOINBACK))
                {
                    auction_joinBackData = (Auction_JoinBackData)intent.getBundleExtra("bundle3").getSerializable("AuctionJoinBack");
                    String code = auction_joinBackData.getCode();
                    if(code!=null&&!code.equals(""))
                    {
                        if(Integer.parseInt(code)>0)
                        {
                            ObjectUtils.toast(mcontext,auction_joinBackData.getMsg());
                        }
                    }

                    if(temporary_listdata!=null) {
                        boolean isupdateusername = false;
                        for (int i = 0; i < temporary_listdata.size(); i++) {
                            int id = Integer.parseInt(temporary_listdata.get(i).getId());
                            if (auction_joinBackData.getId() == id) {
                                if(auction_joinBackData.getUsername()!=null&&auction_joinBackData.getPrice()>0)
                                {
                                    isupdateusername = true;
                                    UpdateAuctionBean update = new UpdateAuctionBean();
                                    update.setUsername(auction_joinBackData.getUsername());
                                    update.setPrice(auction_joinBackData.getPrice());
                                    updateBean.put(temporary_listdata.get(i).getId(),update);
                                    adapter.setCurrentId(temporary_listdata.get(i).getId());
                                    adapter.setUpdateBean(updateBean);
                                    break;
                                }
                            }
                        }
                        if(isupdateusername)
                        {
                            handler1.post(mRunnable3);
                        }
                    }
                }
            }
        };
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(WebSocketService2.ACTION_AUCTION);
        myIntentFilter.addAction(WebSocketService2.ACTION_JOINBACK);
        // myIntentFilter.addAction(WebSocketService.ACTION_Nav);
        //注册广播
        mcontext.registerReceiver(mBroadcastReceiver, myIntentFilter);
    }
    /**
     * 正在趣买适配商品列表
     */
    private void AdapterListData()
    {

        mRefreshLayout.setRefreshing(false);
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
        if(mParam1.equals("100")) {
            mRecyclerView.setVisibility(View.VISIBLE);
            noData.setVisibility(View.INVISIBLE);
            if(auctionactivitydata.getList()!=null&&auctionactivitydata.getList().size()>0)
            {
                adapter.setNewData(listdata);
                adapter.notifyDataSetChanged();
                if (auctionactivitydata.getList().size() < 10) {
                    adapter.loadMoreComplete();
                    adapter.loadMoreEnd();
                }
            }
            else
            {
                noData.setVisibility(View.VISIBLE);
                mRecyclerView.setVisibility(View.INVISIBLE);
                adapter.loadMoreEnd();
            }
        }
        else
        {
            //趣买数据处理,如果这次返回的不足10个
            if(auctionactivitydata.getList().size() < 10)
            {
                isAucDataEnd = true;
                isNeedMerge = true;
                remainsize = 10-auctionactivitydata.getList().size();
                GetDataListData(page,mParam1);
            }
            //这次返回的够10个
            else
            {
                if(auctionactivitydata.getList()!=null) {
                    if (page == 1) {
                        adapter.setNewData(listdata);
                    } else {
                        adapter.addData(listdata);
                    }

                    mRecyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    adapter.loadMoreComplete();
                }
                else
                {
                    adapter.loadMoreEnd();
                }
            }

        }
    }
    private Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:
                    adapter.notifyUsernameDataAndRemove();
                    break;
            }
        }
    };
    //更新当前出价和竞拍人和移除过期的商品
    private Runnable mRunnable3 = new Runnable() {
        @Override
        public void run() {
            if (null == message3) {
                message3 = new Message();
            } else {
                message3 = Message.obtain();
            }
            message3.what = 1;
            //发送信息给handler
            handler1.sendMessage(message3);
        }
    };
    @Override
    protected void setlazyLoad() {
        super.setlazyLoad();
        if (!isPrepared || !isVisible) {
            return;
        }
        page = 1;
        merge_listdata.clear();
        listdata.clear();
        temporary_listdata.clear();
        mRecyclerView.scrollToPosition(0);
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            //正在拍
            if(mParam1.equals("100"))
            {
                auctionActivitySendData.setType("get_auctions");
                auctionActivitySendData.setPage(page);
                auctionActivitySendData.setPer_page(10);
                String mid = LoginState.getInstance().getid(mcontext);
                auctionActivitySendData.setMid(Integer.parseInt(mid));
                gsonstring = gson.toJson(auctionActivitySendData);
                service.sendMsg(gsonstring);
            }
            //全部
            else if(mParam1.equals("0"))
            {
                auctionActivitySendData.setType("get_auctions");
                auctionActivitySendData.setPage(page);
                auctionActivitySendData.setPer_page(10);
                String mid = LoginState.getInstance().getid(mcontext);
                auctionActivitySendData.setMid(Integer.parseInt(mid));
                gsonstring = gson.toJson(auctionActivitySendData);
                service.sendMsg(gsonstring);
            }
            else
            {
                GetDataListData(page, mParam1);
            }
        } else {

            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }

        //刷新
        ReFreshData();
    }
    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        page = 1;
        merge_listdata.clear();
        listdata.clear();
        temporary_listdata.clear();
        if(mParam1.equals("100"))
        {
            auctionActivitySendData.setType("get_auctions");
            auctionActivitySendData.setPage(page);
            auctionActivitySendData.setPer_page(10);
            String mid = LoginState.getInstance().getid(mcontext);
            auctionActivitySendData.setMid(Integer.parseInt(mid));
            gsonstring = gson.toJson(auctionActivitySendData);
            service.sendMsg(gsonstring);
        }
        else if(mParam1.equals("0"))
        {
            auctionActivitySendData.setType("get_auctions");
            auctionActivitySendData.setPage(page);
            auctionActivitySendData.setPer_page(10);
            String mid = LoginState.getInstance().getid(mcontext);
            auctionActivitySendData.setMid(Integer.parseInt(mid));
            gsonstring = gson.toJson(auctionActivitySendData);
            service.sendMsg(gsonstring);
        }
        else
        {
            GetDataListData(page, mParam1);
        }
    }

    @Override
    public void onLoadMoreRequested() {
        page = page + 1;
        if(mParam1.equals("100"))
        {
            auctionActivitySendData.setType("get_auctions");
            auctionActivitySendData.setPage(page);
            auctionActivitySendData.setPer_page(10);
            gsonstring = gson.toJson(auctionActivitySendData);
            service.sendMsg(gsonstring);
        }
        else if(mParam1.equals("0"))
        {
            if(!isAucDataEnd)
            {
                auctionActivitySendData.setType("get_auctions");
                auctionActivitySendData.setPage(page);
                auctionActivitySendData.setPer_page(10);
                gsonstring = gson.toJson(auctionActivitySendData);
                service.sendMsg(gsonstring);
            }
            else {
                GetDataListData(page, mParam1);
            }
        }
        else
        {
            GetDataListData(page, mParam1);
        }
    }
    private void ReFreshData() {
        //设置颜色
        mRefreshLayout.setColorSchemeResources(R.color.app_title);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                {
                    networkConnected = ObjectUtils.isNetworkConnected(getActivity());
                    if (networkConnected) {
                        merge_listdata.clear();
                        listdata.clear();
                        temporary_listdata.clear();
                        page = 1;
                        //正在拍
                        if(mParam1.equals("100"))
                        {
                            auctionActivitySendData.setType("get_auctions");
                            auctionActivitySendData.setPage(page);
                            auctionActivitySendData.setPer_page(10);
                            String mid = LoginState.getInstance().getid(mcontext);
                            auctionActivitySendData.setMid(Integer.parseInt(mid));
                            gsonstring = gson.toJson(auctionActivitySendData);
                            service.sendMsg(gsonstring);
                        }
                        //全部
                        else if(mParam1.equals("0"))
                        {
                            auctionActivitySendData.setType("get_auctions");
                            auctionActivitySendData.setPage(page);
                            auctionActivitySendData.setPer_page(10);
                            String mid = LoginState.getInstance().getid(mcontext);
                            auctionActivitySendData.setMid(Integer.parseInt(mid));
                            gsonstring = gson.toJson(auctionActivitySendData);
                            service.sendMsg(gsonstring);
                        }
                        else
                        {
                            GetDataListData(page, mParam1);
                        }
                    } else {
                        ObjectUtils.toast(getActivity(), "网络连接出现异常");
                        mRefreshLayout.setRefreshing(false);
                    }

                }

            }
        });

    }

    //获取列表数据
    private void GetDataListData(final int page, String type) {
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERAUCTIONORDER;
        MyLog.e("地址", address);
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            HashMap<String, String> header = new HashMap<>();
            HashMap<String, String> map = new HashMap<>();
            map.put("type",type);
            map.put("page",page+"");
            if(isNeedMerge)
            {
                map.put("size", remainsize+"");
            }
            else {
                map.put("size", "10");
            }
            String getid = LoginState.getInstance().getid(mcontext);
            String token = LoginState.getInstance().getUserInfo(mcontext,2);
            MyLog.e("type",type);
            MyLog.e("UID", getid+"");
            MyLog.e("TOKEN", token+"");
            header.put("UID", getid);
            header.put("TOKEN", token);
            HttpUtils.get(address, header, map, new AuctionOrderListDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(AuctionOrderListData response, int id) {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        //不是“全部”数据
                        if(!mParam1.equals("0")) {
                            listdata = new ArrayList<>();
                            noData.setVisibility(View.GONE);
                            mRecyclerView.setVisibility(View.VISIBLE);
                            if (response.getData().getList() != null&&response.getData().getList().size()>0) {
                                listdata.addAll(response.getData().getList());
                                if (page == 1) {
                                    adapter.setNewData(listdata);
                                    mRecyclerView.setAdapter(adapter);
                                } else {
                                    adapter.addData(listdata);
                                    adapter.loadMoreComplete();
                                }
                                if (response.getData().getList().size() < 10) {
                                    adapter.loadMoreEnd();
                                }
                            } else
                            {
                                if(page==1) {
                                    noData.setVisibility(View.VISIBLE);
                                    mRecyclerView.setVisibility(View.GONE);
                                }
                                adapter.loadMoreEnd();
                            }
                        }
                        //“全部”数据
                        else
                        {

                            //趣买数据结束,合并数据
                            if(isNeedMerge)
                            {
                                merge_listdata = new ArrayList<>();
                                if (response.getData().getList() != null) {
                                    merge_listdata.addAll(listdata);
                                    merge_listdata.addAll(response.getData().getList());
                                    if (page == 1) {
                                        adapter.setNewData(merge_listdata);
                                    } else {
                                        adapter.addData(merge_listdata);
                                        adapter.loadMoreComplete();
                                    }
                                    mRecyclerView.setAdapter(adapter);
                                    adapter.notifyDataSetChanged();
                                    if((listdata.size()+response.getData().getList().size())<10)
                                    {
                                        adapter.loadMoreEnd();
                                    }
                                } else {

                                    adapter.loadMoreEnd();
                                }
                                isNeedMerge = false;
                            }
                            //走api的"全部"数据
                            else
                            {
                                merge_listdata = new ArrayList<>();

                                if (response.getData().getList() != null) {
                                    merge_listdata.addAll(response.getData().getList());
                                    if (page == 1) {
                                        adapter.setNewData(merge_listdata);
                                        mRecyclerView.setAdapter(adapter);
                                    } else {
                                        adapter.addData(merge_listdata);
                                        adapter.loadMoreComplete();
                                    }
                                    if (response.getData().getList().size() < 10) {
                                        adapter.loadMoreEnd();
                                    }
                                } else {
                                    if(page==1) {
                                        noData.setVisibility(View.VISIBLE);
                                        mRecyclerView.setVisibility(View.GONE);
                                    }
                                    adapter.loadMoreEnd();
                                }
                            }
                        }
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        // noDataTv.setText(response.getMsg());
                        //noDataTv.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络连接出现异常");
            adapter.loadMoreFail();
        }
    }
//    public class AdaptList extends BaseMultiItemQuickAdapter<AuctionActivityData.ListBean,MytimeViewHolder> {
//        private String type;
//        private FragmentActivity mcontext;
//        private int limitposition;
//        public ArrayList<AuctionActivityData.ListBean> data = new ArrayList();
//
//        public AdaptList(FragmentActivity context, ArrayList<AuctionActivityData.ListBean> data, String type) {
//            super(data);
//            this.data = data;
//            this.mcontext = context;
//            this.type = type;
//            addItemType(AuctionActivityData.ListBean.THREE, R.layout.item_grid_aishangmainpage);
//        }
//
//
//        @Override
//        protected void convert(MytimeViewHolder holder, AuctionActivityData.ListBean mainlistdata) {
//            switch (holder.getItemViewType()) {
//                case AuctionActivityData.ListBean.THREE:
//                        holder.setTime_position(holder.getLayoutPosition());
//                        //判断list里面是否含有该holder，没有就增加
//                        //因为list已经持有holder的引用，所有数据自动会改变
//                        int time = mainlistdata.getTime();
//                        String minute = String.format("%02d", time / 1000 / 60);
//                        String second = String.format("%02d", time / 1000 % 60);
//                        String millsecond = "00";
//                        if (time % 1000 == 0) {
//                            millsecond = String.format("%02d", 99);
//                        } else {
//                            millsecond = String.format("%02d", time % 1000 / 40 * 4);
//                        }
//                        holder.setText(R.id.kill_millsecond, millsecond);
//                        holder.setText(R.id.kill_minute, minute);
//                        holder.setText(R.id.kill_second, second);
//                        ObjectUtils.photo2(mContext, mainlistdata.getThumb(), ((ImageView) holder.getView(R.id.item_grid_aishang_img)));
//                        // holder.setText(R.id.rightnow_phone, mainlistdata.getUsername());
//                        holder.setText(R.id.item_rightnow_price, "当前价: ￥" + mainlistdata.getPrice());
//                        holder.setText(R.id.item_grid_detail_like_title, mainlistdata.getTitle());
//                        holder.addOnClickListener(R.id.rightnow_openteam);
//                    break;
//            }
//        }
//    }



    private void setCancel(String url, final int position) {
        progressLoading.setVisibility(View.VISIBLE);

        String getid = LoginState.getInstance().getid(mcontext);
        String getpd = LoginState.getInstance().getpd(mcontext);

        HashMap<String, String> header = new HashMap<>();

        header.put("UID", getid);
        header.put("UPSW", getpd);
        HttpUtils.get(url, header, null, new dataCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {
                progressLoading.setVisibility(View.GONE);
            }

            @Override
            public void onResponse(data response, int id) {
                progressLoading.setVisibility(View.GONE);
                ObjectUtils.toast(mcontext, response.getMsg());
                if (response.getCode() == 0) {
                    adapter.remove(position);
                    if (adapter.getData().size() == 0) {
                        noData.setVisibility(View.VISIBLE);
                    }


                }

            }
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mParam1.equals("100")||mParam1.equals("0")) {
            service = null;
            getActivity().unregisterReceiver(mBroadcastReceiver);
            mBroadcastReceiver=null;
            handler1.removeCallbacks(mRunnable3);
        }
    }
}

